$(document).ready(function () {
    var url = "http://127.0.0.1:8080/homework_war_exploded/";

    function init() {
      // $("#sea").click(function () {
      $.ajax({
        type: "get",
        url: url + "user/getUserList.do",
        dataType: "json",
        success: function (v) {
          var table = "<tr><th>序号</th><th>ID</th><th>账号</th><th>角色</th><th>姓名</th><th>邮箱</th><th>所属学院</th><th>专业</th></tr>";
          if (v.success) {
            // layer.alert("");
            $.each(v.data, function (index, d) {
              table += '<tr class="success">'
                + '<td>' + (index + 1) + '</td>'
                + '<td>' + d.user_id + '</td>'
                + '<td>' + d.user_account + '</td>'
                // + '<td>' + d.user_pwd + '</td>'
                + '<td>' + d.user_role + '</td>'
                + '<td>' + d.user_name + '</td>'
                + '<td>' + d.user_email + '</td>'
                + '<td>' + d.user_college + '</td>'
                + '<td>' + d.course_id + '</td>'
                //+ '<td>' + d.user_newpwd + '</td>'
                + '</tr>'
            });
            $("#tb").html(table);
          } else {
            layer.alert("查询失败!");
          }
        }
      })
  }
  init();


  $("#save").click(function () {
    var account = $("#account").val();
    var userpwd = $("#userpwd").val();
    var radio = $("input[type='radio']:checked").val();
    var username = $("#username").val();
    var inputEmail3 = $("#inputEmail3").val();
    var user_college = $("#user_college").val();
    var cur = $("#cur").val();
    $.ajax({
      type: "post",
      url: url + "/admin/addUser.do",
      dataType: "json",
      data: {
        user_account: account,
        user_pwd: userpwd,
        user_role: radio,
        user_name: username,
        user_email: inputEmail3,
        user_college: user_college,
        course_id: cur
      },
      success: function (a) {
        if(a.success){
          init();
          layer.alert("添加成功!");
          //$('#myModal').fadeOut();
        }else {
          layer.alert("添加失败!");
        }
      }
    })
  });

  //页面层
  $("#add").click(function () {
  layer.open({
    type: 1,
    skin: 'layui-layer-rim', //加上边框
    area: ['200px', '200px'], //宽高
    content: '<div class="modal-content">\n' +
      '      <div class="modal-body">\n' +
      '        <form class="form-horizontal">\n' +
      '          <div class="form-group">\n' +
      '            <label for="inputEmail3" class="col-sm-2 control-label">账号</label>\n' +
      '            <div class="col-sm-10">\n' +
      '              <input type="email" class="form-control" id="account" placeholder="账号">\n' +
      '            </div>\n' +
      '          </div>\n' +
      '          <div class="form-group">\n' +
      '            <label for="inputPassword3" class="col-sm-2 control-label">密码</label>\n' +
      '            <div class="col-sm-10">\n' +
      '              <input type="password" class="form-control" id="userpwd" placeholder="Password">\n' +
      '            </div>\n' +
      '          </div>\n' +
      '          <div class="form-group">\n' +
      '            <label for="inputPassword3" class="col-sm-2 control-label">角色</label>\n' +
      '            <div class="radio">\n' +
      '              <label>\n' +
      '                <input type="radio" name="optionsRadios" id="admin" value="1" checked>管理员\n' +
      '              </label>\n' +
      '              <label>\n' +
      '                <input type="radio" name="optionsRadios" id="teacher" value="2" >教师\n' +
      '              </label>\n' +
      '            </div>\n' +
      '          </div>\n' +
      '          <div class="form-group">\n' +
      '            <label for="inputPassword3" class="col-sm-2 control-label">姓名</label>\n' +
      '            <div class="col-sm-10">\n' +
      '              <input type="text" class="form-control" id="username" placeholder="姓名">\n' +
      '            </div>\n' +
      '          </div>\n' +
      '          <div class="form-group">\n' +
      '            <label for="inputEmail3" class="col-sm-2 control-label">Email</label>\n' +
      '            <div class="col-sm-10">\n' +
      '              <input type="email" class="form-control" id="inputEmail3" placeholder="Email">\n' +
      '            </div>\n' +
      '          </div>\n' +
      '          <div class="form-group">\n' +
      '            <label for="inputEmail3" class="col-sm-2 control-label">所属学校</label>\n' +
      '            <div class="col-sm-10">\n' +
      '              <input type="email" class="form-control" id="user_college" placeholder="学校">\n' +
      '            </div>\n' +
      '          </div>\n' +
      '          <div class="form-group">\n' +
      '            <label for="inputEmail3" class="col-sm-2 control-label">所授课程</label>\n' +
      '            <div class="col-sm-10">\n' +
      '              <select id="cur" class="form-control">\n' +
      '                <option value="">请选择</option>\n' +
      '                <option value="1">C#</option>\n' +
      '                <option value="2">JAVA</option>\n' +
      '                <option value="3">DBA</option>\n' +
      '                <option value="4">语文</option>\n' +
      '                <option value="5">数学</option>\n' +
      '              </select>\n' +
      '            </div>\n' +
      '          </div>\n' +
      '        </form>\n' +
      '      </div>\n' +
      '      <div class="modal-footer">\n' +
      '        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>\n' +
      '        <button type="button" class="btn btn-primary" id="save">确认添加</button>\n' +
      '      </div>\n' +
      '    </div>'
  });
  })


})

